package com.shopping.mall.core.module.goodsSpecification.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shopping.mall.base.http.ResponseState;
import com.shopping.mall.core.module.goodsSpecification.vo.GoodsSpecificationVo;
import com.shopping.mall.auth.annotation.UnLogin;
import com.shopping.mall.base.http.Result;
import com.shopping.mall.core.module.goodsSpecification.entity.GoodsSpecification;
import com.shopping.mall.core.module.goodsSpecification.entity.NumPrice;
import com.shopping.mall.core.module.goodsSpecification.service.GoodsSpecificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "GoodsSpecification")
@RestController
@RequestMapping("/goodsSpecification")
public class GoodsSpecificationController {

    @Autowired
    private GoodsSpecificationService goodsSpecificationService;
    @Autowired
    private Logger logger;

    @ApiOperation("添加(修改)规格,数量改变价格")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    @PostMapping("/add")
    public Result<Object> addGoodsSpecification(@Validated @RequestBody GoodsSpecificationVo goodsSpecificationVo) {
        try {
            return goodsSpecificationService.addGoodsSpecification(goodsSpecificationVo);
        } catch (RuntimeException e) {
            logger.error("事务异常,终止操作:{}", e.getStackTrace());
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("根据规格信息返回规格")
    @UnLogin
    @PostMapping("/specificationInfo")
    public Result<Object> specificationInfo(@RequestBody List<Map<String, String>> specification) {
        return goodsSpecificationService.specificationInfo(specification);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<IPage<GoodsSpecification>> getGoodsSpecificationPage(
            @RequestParam(name = "current", defaultValue = "1") Integer current,
            @RequestParam(name = "size", defaultValue = "10") Integer size,
            GoodsSpecification goodsSpecification) {
        IPage<GoodsSpecification> goodsSpecificationPage = goodsSpecificationService.getGoodsSpecificationPage(current, size, goodsSpecification);
        return Result.OK(goodsSpecificationPage);
    }

    @ApiOperation("修改(不用了)")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    @Deprecated
    @PutMapping
    public Result<GoodsSpecification> update(@Validated(GoodsSpecification.update.class) @RequestBody GoodsSpecification goodsSpecification) {
        goodsSpecificationService.updateById(goodsSpecification);
        return Result.OK();
    }

    @ApiOperation("新增(不用了)")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    @Deprecated
    @PostMapping
    public Result<Object> add(@Validated(GoodsSpecification.insert.class) @RequestBody GoodsSpecification goodsSpecification) {
        goodsSpecificationService.save(goodsSpecification);
        return Result.OK();
    }

    @ApiOperation("通过id查询")
    @GetMapping("/id")
    public Result<GoodsSpecification> getById(@RequestParam String id) {
        GoodsSpecification goodsSpecification = goodsSpecificationService.getById(id);
        return Result.OK(goodsSpecification);
    }

    @ApiOperation("查询所有")
    @GetMapping
    public Result<List<GoodsSpecification>> getAll(GoodsSpecification goodsSpecification) {
        List<GoodsSpecification> goodsSpecificationList = goodsSpecificationService.getAll(goodsSpecification);
        return Result.OK(goodsSpecificationList);
    }

    @ApiOperation("通过id删除")
    @PreAuthorize("hasAnyRole('SUPER_ADMIN','NORMAL_ADMIN')")
    @DeleteMapping("/id")
    public Result<Object> deleteById(@RequestParam String id) {
        return goodsSpecificationService.deleteById(id);
    }

    @ApiOperation("批量删除")
    @DeleteMapping("/batch")
    public Result<Object> batchDelete(@RequestParam List<String> ids) {
        return goodsSpecificationService.batchDelete(ids);
    }
}
